package com.shop.shoporder.wapController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserLoginToken;
import com.shop.shoporder.entity.order.Cart;
import com.shop.shoporder.entity.order.CartList;
import com.shop.shoporder.service.CartService;
import com.shop.shoporder.service.GoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/wap")
public class GoodsCommentController {
    @Autowired
    GoodsCommentService goodsCommentService;
@Autowired
    CartService cartService;
    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/goodsComment",method = RequestMethod.POST)
    //商品评论
    public Response goodsComment(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            response=goodsCommentService.goodsComment(map,request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
        return response;
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/commentList",method = RequestMethod.GET)
    //商品评论
    public Response commentList(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{
            List<Cart> cartLists=new ArrayList<>();
            PageInfo<Cart> cartPageInfo=new PageInfo<>(cartLists);
            int pages = 0;
            int limits = 5;
            if(map.get("page")!=null){
                pages=Integer.parseInt(map.get("page"));
            }
            if(map.get("limit")!=null){
                limits=Integer.parseInt(map.get("limit"));
            }
            PageHelper.startPage(pages, limits);
            List<Cart> cartList=cartService.getOrderCommentGoodsList(map,request);
            cartPageInfo=new PageInfo<>(cartList);
            if(cartList!=null&&cartList.size()>0){
                return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),cartPageInfo.getTotal(),"获取数据成功！",request);
            }else{
                return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),0,"获取数据失败！",request);
            }
            //return new Response(MessageConstants.MSG_SUCCESS,cartPageInfo.getList(),cartPageInfo.getList().size(),"获取成功！",request);
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
    }

}
